package com.syj.collect.Present;

import android.content.Intent;
import android.os.DropBoxManager;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.syj.collect.base.BasePresent;
import com.syj.collect.http.ApiServise;
import com.syj.collect.http.BaseObserver;
import com.syj.collect.http.BaseRequest;
import com.syj.collect.utils.SPUtil;
import com.syj.collect.view.CollectActivityView;
import com.syj.collect.view.LoginView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class LoginPresent extends BasePresent<LoginView> {


    public void Login(String name, String pwd) {
        HashMap hashMap = new HashMap();
        hashMap.put("password", pwd);
        hashMap.put("userName", name);
        //hashMap.put("captcha", "");
        //hashMap.put("uuid", "");


//        String md5string=StringUtils.initString(hashMap);
//        hashMap.put("sign",md5string);

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), hashMapToJson(hashMap));
        // Log.e("1", str);
        BaseRequest.getInstance().getApiServise().Login(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public boolean onSuccess(String value) {
                        try {
                            JSONObject jsonObject = new JSONObject(value);
//                            Log.e("结果 :", value);
                            String msg = jsonObject.getString("msg");
                            int code = jsonObject.getInt("code");
                            if (code == 0) {
                                view.initSuccess(msg);
                                SPUtil.setPrefString("userInfo", jsonObject.getString("userInfo"));
                                SPUtil.setPrefString("token", jsonObject.getString("token"));
//                                Log.e("sp",SPUtil.getPrefString("token","1"));
                            } else {
                                view.initFail(msg);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        return false;
                    }

                    @Override
                    public void onFailure(Throwable e) throws Exception {
                        view.initFail("网络异常");
                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }


    public static String hashMapToJson(HashMap map) {

        Gson gson = new Gson();
        String str = gson.toJson(map);


       /* String string = "{";
        for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry e = (Map.Entry) it.next();
            string += "'" + e.getKey() + "':";
            string += "'" + e.getValue() + "',";
        }
        string = string.substring(0, string.lastIndexOf(","));
        string += "}";
        return string;*/
        return str;
    }


}
